package com.example.online.dao;

import com.example.online.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


import java.util.List;

public interface OnLineDao {


    //查找账户用户名
    @Select("SELECT count(*) FROM user WHERE name = #{name}")
    int isExistByName(String name);

    //查找账户手机号
    @Select("SELECT count(*) FROM user WHERE phone = #{phone}")
    int isExistByPhone(String phone);

    //查看账户是否已被锁定
    @Select("SELECT (UNIX_TIMESTAMP() - timestamp) FROM user WHERE name = #{name}")
    int isLock(String name);

    //查找用户名及密码组合
    @Select("SELECT count(*) FROM user WHERE name = #{name} && password = #{pwd}")
    int login(@Param("name") String name,@Param("pwd") String pwd);

    //账户登陆错误次数加1
    @Update("update  user set login_error = login_error + 1 where name = #{name}")
    void updateLoginErrorAdd1(String name);

    //查看账户失败次数
    @Select("select login_error from user where name = #{name}")
    int findLoginError(String name);

    //将用户登陆错误次数归零
    @Update("update  user set login_error = 0 where name = #{name}")
    void updateLoginError0(String name);

    //将用户账户锁定
    @Update("update  user set timestamp = unix_timestamp() where name = #{name}")
    void lockOfName(String name);


    //账户累计登陆次数加1
    @Update("update user set login_number = login_number + 1 where name = #{name}")
    void updateLoginNumberAdd1(String name);

    //添加账户
    @Insert("insert into user (phone,name,password) values (#{phone},#{name},#{pwd})")
    void insert(@Param("phone")String phone,@Param("name") String name,@Param("pwd") String pwd);

    //登陆成功获取账户信息
    @Select("select name,phone,password as pwd,login_number as loginNumber from user where name = #{name} && password = #{pwd}")
    User getUser(@Param("name")String name,@Param("pwd")String pwd);


    //忘记密码
    @Update("update user set password = #{pwd} where phone = #{phone}")
    int updatePwdByPhone(@Param("pwd")String pwd,@Param("phone")String phone);
}


//create table user (
//    id int primary key auto_increment comment "主键，自增",
//    name varchar(10) not null unique comment "姓名，非空且唯一",
//    phone char(11) not null unique comment "手机号，非空且唯一",
//    password varchar(30) not null comment "密码，输入错误则错误次数加1",
//    timestamp bigint default 0 comment "时间戳，实现锁定账户功能",
//    login_error int default 0 comment "错误次数，失败3次锁定5分钟"
//)comment "客户账户表";
